<?php
/**
 * Created by PhpStorm.
 * User: cyc
 * Date: 2017/10/30
 * Time: 下午8:28
 */

abstract class BundlerBase implements BundleInterface
{
    use util_person;
    use util_array;
    protected $_notice = true;
    protected $bundleTimeStart = null;
    protected $bundleTimeEnd = null;
    protected $updateBundleItem = true;


    public function __construct($bundleTimeStart = null, $bundleTimeEnd = null, $notice = true, $updateBundleItem = true)
    {
        $this->updateBundleItem = $updateBundleItem;
        $this->bundleTimeStart = empty($bundleTimeStart) ? time() - 86400 : $bundleTimeStart;
        $this->bundleTimeEnd = empty($bundleTimeEnd) ? time() : $bundleTimeEnd;
        $this->_notice = $notice;
    }


    public function bundle()
    {
        $datas = $this->getBindableDatas();
        foreach ($datas as $data) {
            $bitem = $this->getBundleItem($data);
            if ($bitem && $bitem->save()) {
                $bitem->execute();
                if ($this->_notice) {
                    $this->notice($data, $bitem);
                }
            } else {
                throw new CException('bundle msg save failed', 1);
            }
        }
        return $datas;
    }

    function genBundleContent($data, $bitem, $isHtml = true)
    {
        $url = Yii::app()->createAbsoluteUrl('msgg/item/manage', ['MsggItem' => ['bid' => $bitem->id]]);;
        $params = self::util_array_get($data, 'params', []);
        $lines = array();
        if (is_array($params)) {
            foreach ($params as $param) {
                $value = self::util_array_get($param, 'value', '');
                $label = self::util_array_get($param, 'label', $value);
                $lines[] = "【" . $label . "】:" . $value;
            }
        } elseif (is_string($params)) {
            $lines[] = $params;
        } else {
            $lines[] = '';
        }
        $lines[] = "【备注】:如有任何疑问请联系管理员寻求帮助 " . self::util_admin_mail();
        $lines[] = $isHtml ? '<a href="' . $url . '">我要查看</a>' : ("我要查看:" . $url . "\n");
        return implode(($isHtml ? '<br>' : "\n"), $lines);
    }

    function notice($data, MsggItem $bitem)
    {
        $content = $this->genBundleContent($data, $bitem, false);
        if ($bitem->pid) {
            $person = AlarmPerson::model()->findByPk($bitem->pid);
            if ($person && $person->wechat && $person->wechat_status) {
                $person->msg(['name' => $person->wechat, 'wechat' => ['content' => $content]], MsggItem::TYPE_WECHAT, ['level' => MsggItem::INNER_LEVEL]);
            } else {
                Yii::log($bitem->pid . ' wechat is invalidate', CLogger::LEVEL_INFO,   __METHOD__);
            }
        }

    }

    function getBundleItem($data)
    {
        $pid = self::util_array_get($data, 'pid', null);
        $ids = self::util_array_get($data, 'ids', null);
        $title = self::util_array_get($data, 'title', 'Msgg 提示您!');
        if (!$pid || empty($ids) || empty($person = AlarmPerson::model()->findByPk($pid))) {
            throw new CException('pid and ids should be contained in data', 1);
        } elseif (!is_array($ids)) {
            throw new CException('ids should be array', 1);
        }
        $bitem = MsggItem::build(MsggItem::TYPE_MAIL, [], ['pid' => $pid, 'level' => MsggItem::INNER_LEVEL,]);
        if ($bitem && $bitem->save()) {
            $content = $this->genBundleContent($data, $bitem, true);
            $from = 'msgg';
            $bitem->set_Params(['from'=>$from,'address' => $person->mail, 'title' => $title, 'content' => $content,]);
            $bitem->save();
            $criteria = new CDbCriteria();
            $criteria->addInCondition('id', $ids);
            $update = array('bid' => $bitem->id);
            if ($this->updateBundleItem)
            {
                $update['status']=MsggItem::STATUS_BUNDLED;
            }
            MsggItem::model()->updateAll($update,$criteria);
        } else {
            Yii::log('save failed', CLogger::LEVEL_ERROR,   __METHOD__);
        }
        return $bitem;
    }

}